<?php
	class Setting extends CI_Controller
	{
		public function __construct()
		{
			parent::__construct();
			if (!isset($_SESSION)) {
			    session_start();
			    session_regenerate_id();
			}
			$this->load->helper("url");
			
		}
		public function index()
		{
			$this->load->model("mcategory");
			
			$data['action']='list_main_category';
			$data['list_main_category']=$this->mcategory->list_main_category();
			
			$this->load->view("setting_template", $data);
		}
		public function category_detail()
		{
			$this->load->model("mcategory");
			$cid=$_REQUEST['cid'];
			$data['action']='category_detail';
			$data['list_category_detail']=$this->mcategory->list_category_detail($cid);
			$data['main_category']=$this->mcategory->main_category($cid);
			$data['list_main_category']=$this->mcategory->list_main_category();
			
			$this->load->view("setting_template", $data);
		}
		public function add_category_detail()
		{
			$this->load->model("mcategory");
			
			$pos=$_REQUEST['pos'];
			$nameVN=$_REQUEST['nameVN'];
			$cid=$_REQUEST['cid'];
			
			$this->mcategory->add_category_detail($pos,$nameVN,$cid);
			
			echo '<script>
						window.location.assign("'.base_url('setting/category_detail?cid='.$cid).'");
						</script>';
		}
		public function delcategory_detail()
		{
			$this->load->model("mcategory");
			
			$cid=$_REQUEST['cid'];
			$cid1=$_REQUEST['cid1'];
			
			$this->mcategory->delcategory($cid);
			
			echo '<script>
						window.location.assign("'.base_url('setting/category_detail?cid='.$cid1).'");
						</script>';
		}
		public function edit_category_detail()
		{
			
			$this->load->model("mcategory");
			$cid=$_REQUEST['cid'];
			$id_category=array();
			foreach ($_REQUEST['id_category'] as $value) {
			    $id_category[]=$value;
			}
			
			$pos=array();
			foreach ($_REQUEST['pos'] as $value) {
			    $pos[]=$value;
			}
			$nameVN=array();
			foreach ($_REQUEST['nameVN'] as $value) {
			    $nameVN[]=str_replace( ',', '', $value);
			}
			
			$parent=array();
			foreach ($_REQUEST['parent'] as $value) {
			    $parent[]=$value;
			}
			$i=0;
			foreach ($id_category as $value){
			$this->mcategory->edit_category_detail($pos[$i],$nameVN[$i],$id_category[$i],$parent[$i]);
			$i++;
			}
			echo '<script>
						window.location.assign("'.base_url('setting/category_detail?cid='.$cid).'");
						</script>';
		}
		
		
		
		
		
		public function delcategory()
		{
			$this->load->model("mcategory");
			
			$cid=$_REQUEST['cid'];
			
			$this->mcategory->delcategory($cid);
			
			echo '<script>
						window.location.assign("'.base_url('setting').'");
						</script>';
		}
		public function add_main_category()
		{
			$this->load->model("mcategory");
			
			$pos=$_REQUEST['pos'];
			$nameVN=$_REQUEST['nameVN'];
			
			$this->mcategory->add_main_category($pos,$nameVN);
			
			echo '<script>
						window.location.assign("'.base_url('setting').'");
						</script>';
		}
		
		public function edit_main_category()
		{
			
			$this->load->model("mcategory");
			
			$id_category=array();
			foreach ($_REQUEST['id_category'] as $value) {
			    $id_category[]=$value;
			}
			
			$pos=array();
			foreach ($_REQUEST['pos'] as $value) {
			    $pos[]=$value;
			}
			$nameVN=array();
			foreach ($_REQUEST['nameVN'] as $value) {
			    $nameVN[]=str_replace( ',', '', $value);
			}
			$i=0;
			foreach ($id_category as $value){
			$this->mcategory->edit_main_category($pos[$i],$nameVN[$i],$id_category[$i]);
			$i++;
			}
			echo '<script>
						window.location.assign("'.base_url('setting').'");
						</script>';
		}
		
	}
?>